/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.helper;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

import java.util.Set;

/**
 * Global data help class(Preferences)
 *
 * @author Kirky
 * @version 1.0
 * @date 2021/11/10
 */
public class PreferencesHelper {
    private final String DEFAULT_NAME = "luciole";
    private static Context context;
    private DatabaseHelper databaseHelper;
    private Preferences preferences;


    public PreferencesHelper() {
        if (this.preferences == null) {
            if (this.databaseHelper == null) {
                this.databaseHelper = new DatabaseHelper(context);
            }
            preferences = databaseHelper.getPreferences(DEFAULT_NAME);
        }
    }

    public static PreferencesHelper getInstance(Context context) {
        if (PreferencesHelper.context == null) {
            PreferencesHelper.context = context;
        }
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final PreferencesHelper INSTANCE = new PreferencesHelper();
    }

    /**
     * Store data synchronously (String)
     *
     * @param key   Indexes
     * @param value 字符串
     * @return Is the storage successful?
     */
    public boolean putSync(String key, String value) {
        preferences.putString(key, value);
        return preferences.flushSync();
    }

    /**
     * Store data synchronously (Int)
     *
     * @param key   Indexes
     * @param value int
     * @return Is the storage successful?
     */
    public boolean putSync(String key, int value) {
        preferences.putInt(key, value);
        return preferences.flushSync();
    }

    /**
     * Store data synchronously (Boolean)
     *
     * @param key   Indexes
     * @param value bool
     * @return Is the storage successful?
     */
    public boolean putSync(String key, boolean value) {
        preferences.putBoolean(key, value);
        return preferences.flushSync();
    }

    /**
     * Store data synchronously (Float)
     *
     * @param key   Indexes
     * @param value float
     * @return Is the storage successful?
     */
    public boolean putSync(String key, float value) {
        preferences.putFloat(key, value);
        return preferences.flushSync();
    }

    /**
     * Store data synchronously (Long)
     *
     * @param key   Indexes
     * @param value long
     * @return Is the storage successful?
     */
    public boolean putSync(String key, long value) {
        preferences.putLong(key, value);
        return preferences.flushSync();
    }

    /**
     * Store data synchronously (Set<String> )
     *
     * @param key   Indexes
     * @param value Set<String>
     * @return Is the storage successful?
     */
    public boolean putSync(String key, Set<String> value) {
        preferences.putStringSet(key, value);
        return preferences.flushSync();
    }

    /**
     * get data
     *
     * @param key index
     * @return String
     */
    public String getString(String key) {
        return preferences.getString(key, "");
    }

    /**
     * get data
     *
     * @param key index
     * @return int
     */
    public int getInt(String key) {
        return preferences.getInt(key, 0);
    }

    /**
     * get data
     *
     * @param key index
     * @return bool
     */
    public boolean getBoolean(String key) {
        return preferences.getBoolean(key, false);
    }

    /**
     * get data
     *
     * @param key index
     * @return float
     */
    public float getFloat(String key) {
        return preferences.getFloat(key, 0f);
    }

    /**
     * get data
     *
     * @param key index
     * @return long
     */
    public long getLong(String key) {
        return preferences.getLong(key, 0L);
    }

    /**
     * get data
     *
     * @param key index
     * @return Set<String>
     */
    public Set<String> getSet(String key) {
        return preferences.getStringSet(key, null);
    }

    /**
     * Delete database
     **/
    public void delete() {
        preferences.delete(DEFAULT_NAME);
    }
}
